package com.lei.sort;

public class SortingHelper2 {

    private SortingHelper2() {
    }

    public static <E extends Comparable<E>> boolean isSorted(E[] arr) {

        for (int i = 1; i < arr.length; i++)
            if (arr[i - 1].compareTo(arr[i]) > 0)
                return false;
        return true;
    }

    public static <E extends Comparable<E>> void sortTest(String sortname, E[] arr) {

        long startTime = System.nanoTime();
        if (sortname.equals("MergeSort"))
            MergeSort.sort(arr);
        else if (sortname.equals("MergeSortBU"))
            MergeSort.sortBU(arr);
        else if (sortname.equals("QuickSort"))
            QuickSort.sort(arr);
        else if (sortname.equals("HeapSort"))
            HeapSort.sort(arr);
        else if (sortname.equals("LSDSort")) {
            String[] strArr = (String[]) arr;
            if (strArr.length == 0) throw new IllegalArgumentException("arr can not be empty.");
            LSDSort.sort(strArr, strArr[0].length());
        } else if (sortname.equals("BucketSort")) {
            Integer[] intArr = (Integer[]) arr;
            BucketSort.sort(intArr, 200);
        } else if (sortname.equals("BucketSort2")) {
            Integer[] intArr = (Integer[]) arr;
            BucketSort.sort2(intArr, 100);
        }
        long endTime = System.nanoTime();

        double time = (endTime - startTime) / 1000000000.0;

        if (!SortingHelper.isSorted(arr))
            throw new RuntimeException(sortname + " failed");
        System.out.println(String.format("%s , n = %d : %f s", sortname, arr.length, time));
    }
}